package com.chixing.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Wesley
 * @version 1.0
 * @date 2021/8/9 11:12
 */
public class DateUtil {
    //年月日时分秒
    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";

    //年月日时分
    public static final String YMDHM = "yyyy-MM-dd HH:mm";

    //年月日
    public static final String YMD = "yyyy-MM-dd";

    //时分秒
    public static final String HMS = "HH:mm:ss";

    //时分
    public static final String HM = "yyyy-MM-dd HH:mm";

    //Date类型转换成String类型
    public static String dateToStr(Date date,String pattern){
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        String s = dateFormat.format(date);
        return s ;
    }

    //String转换成Date类型
    public static Date stringToDate(String date,String pattern){
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        Date date1 = null;
        try {
            date1 = dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }
}
